var ws = null;
function startWebSocket() {
	var i_status = document.getElementById("i_status").value;
	if(i_status == 1){
		ws.close();
		return;
	}
	var phone = document.getElementById('phone').value;
	var password = document.getElementById('password').value;
	if ('WebSocket' in window){
		ws = new WebSocket("ws://192.168.6.155:8888/WsDemo/connect/"+phone,password);
		console.info("onclose-ws:"+Object.keys(ws));
	}else if ('MozWebSocket' in window){
		ws = new MozWebSocket("ws://192.168.6.155:8888/WsDemo/connect/"+phone,password);
		console.info("onclose-ws:"+Object.keys(ws));
	}else{
		alert("您的浏览器不支持Websocket");
	}
	ws.onmessage = function(evt) {
		setMessageInnerHTML(evt.data);
	};
	function setMessageInnerHTML(innerHTML) {
		$('#div_msg_content').append("<div style='width: 360px; background-color: #FFECF5;'>"+innerHTML+"</div>");
		var div = document.getElementById('div_msg_content');
		div.scrollTop = div.scrollHeight;
	}
	ws.onclose = function(evt) {
		console.info("onclose:"+evt.code);
		console.info("onclose:"+evt.reason);
		document.getElementById("i_status").value="0";
		document.getElementById("a_status").innerHTML = "已断开，点击连接";
		if(evt.code == 4003){
			$('#div_msg_content').append("<div style='width: 360px; background-color: red;'>"+evt.reason+"</div>");
		}
	};
	ws.onopen = function(evt) {
		console.info("onopen:"+ws.readyState);
		console.info("onopen:"+JSON.stringify(evt));
		document.getElementById("i_status").value="1";
		document.getElementById("a_status").innerHTML = "已连接，点击断开";
	};
	
	ws.onerror = function(errorEvent) {
	    console.log("onerror: ", errorEvent);
	};
}
function sendMsg() {
	var i_status = document.getElementById("i_status").value;
	if(i_status == 0){
		alert("请先连接");
	}
	var fromName = document.getElementById('phone').value;
	var content = document.getElementById('writeMsg').value; //发送内容
	ws.send(fromName + "：" + content);//注意格式
	$('#div_msg_content').append("<div style='width: 360px; background-color: #FFF7FB;'>我："+content+"</div>");
	var div = document.getElementById('div_msg_content');
	div.scrollTop = div.scrollHeight;
	document.getElementById('writeMsg').value = "";
}